package leetcode_core.leetcode_1;

import java.util.ArrayList;
import java.util.List;

public class Subsets2 {
    private List<List<Integer>> ans = new ArrayList<List<Integer>>();
    public List<List<Integer>> subsets(int[] nums) {
        List<Integer> track = new ArrayList<>();
        backtrack(track,0,nums);
        return ans;
    }
    //backtrack(路径,选择列表)
    private void backtrack(List<Integer> list,int start,int[] nums){
        //前序遍历的位置
        ans.add(copy(list));
        //从start开始,避免产生重复的子集
        for(int i = start;i<nums.length;i++){
            //做选择
            list.add(nums[i]);
            //递归回溯
            backtrack(list,i+1,nums);
            //撤销选择
            list.remove(list.size()-1);
        }
    }
    private List<Integer> copy(List<Integer> raw){
        List<Integer> newList = new ArrayList<>(raw);
        return newList;
    }

}
